
<%@include file="init.jsp"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<div class="master-wrapper-content">
	<jsp:include page="heading.jsp"></jsp:include>
	<jsp:include page="listCategory.jsp"></jsp:include>
	<div class="container-right">
		<%
			List<Product> products = null;
		
			if(request.getParameter("type") != null)
			{
				products = (List<Product>) session.getAttribute("search");
				session.removeAttribute("search");
			}
			else
			{
				if (request.getParameter("categoryId") == null) {
					products = dataManager.getRecurisveProductWithCategoryId(0, 20);
				}
				
				else {
					products = dataManager.getRecurisveProductWithCategoryId(Integer
							.valueOf(request.getParameter("categoryId")), 20);
				}
			}

			if (products == null || products.isEmpty()) {
		%><h3 style="color: red">Khong co san pham duoc tim thay</h3>
		<%
			} 
			
			else {
		%>
		<div class="ml-banner">
			<div class="ml-title">Các sản phẩm</div>
		</div>
		<table  id="table-product">
			<%
				int i = 0;
					int size = products.size();
					int length = size - (size % 4);
					while (i < length) {
			%>
			<tr>
				<%
					for (int j = 0; j < 4; j++) {
				%>
				<td id="product-item">
					<table style="width: 210px">
						<tr>
							<td id="product-name"><%=products.get(i + j).getProductName()%></td>
						</tr>
						<tr id="product-image">
							<td><a
								href="productDetail.jsp?productId=<%=products.get(i + j).getProductId()%>"><img
									src="<%="/ECommerce/DisplayImage?path="
								+ products.get(i + j).getImage()%>" />
							</a>
							</td>
						</tr>
						<tr id="product-chitiet">
							<td><a class="chitiet"
								href="productDetail.jsp?productId=<%=products.get(i + j).getProductId()%>">Chi
									tiết</a>
							</td>
						</tr>
						<tr>
							<td id="gia">Gía : <%=products.get(i + j).getPrice()%></td>
						</tr>
					</table></td>
				<%
					}
				%>
			</tr>
			<%
				i += 4;
					}
					if ((size % 4) > 0) {
			%>
			<tr>
				<%
					length = size % 4;
					for (int j = 0; j < length; j++) {
				%>
				<td id="product-item">
					<table>
						<tr>
							<td id="product-name"><%=products.get(i + j).getProductName()%></td>
						</tr>
						<tr id="product-image">
							<td><a
								href="productDetail.jsp?productId=<%=products.get(i + j).getProductId()%>"><img
									src="<%="/ECommerce/DisplayImage?path="
								+ products.get(i + j).getImage()%>" />
							</a>
							</td>
						</tr>
						<tr id="product-chitiet">
							<td><a class="chitiet"
								href="productDetail.jsp?productId=<%=products.get(i + j).getProductId()%>">Chi
									tiết</a>
							</td>
						</tr>
						<tr>
							<td id="gia">Gía : <%=products.get(i + j).getPrice()%></td>
						</tr>
					</table></td>
				<%
					}
				%>
			</tr>
			<%
				}
			%>
		</table>
		<%
			}
		%>
	</div>
	<div class="clear"></div>
	<jsp:include page="footer.jsp"></jsp:include>
</div>